package cn.bopomo.dealer.base.action.ajax;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import cn.bopomo.dealer.base.action.BaseAction;
import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.vo.AuthorizeList;

public class LoadModuleAuthorizeListAjax extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//AJAX返回设置
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		//获取参数
		Integer mId = new Integer(request.getParameter("mid"));
		//获取Service
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		//获取"权限"列表
		List authorizesList = os.getAuthorizesByModule(mId);
		//
		if (authorizesList != null && authorizesList.size()>0){
			JSONArray  array = this.jsonProblems(authorizesList);
			//建立JSON实例
			JSONObject json = new JSONObject();
			json.put("authorizes", array);
			//返回JSON对象
			response.getWriter().write(json.toString());
			return null;
		}else{
			response.getWriter().write("");
			return null;
		}
		
	}
	

	protected JSONArray jsonProblems(List authorizesList) {
		JSONArray  array = new JSONArray();
		for (int i = 0;i<authorizesList.size();i++){
			AuthorizeList tmp = (AuthorizeList)authorizesList.get(i);
			JSONObject json = new JSONObject();
			json.put("authorizeId",tmp.getAid().toString());
			json.put("authorizeName", tmp.getAuthorizeName());
			array.put(json);
		}
		return array;
	}
}
